package messenger;

import static org.junit.Assert.*;

import java.util.*;
import org.junit.*;

public class UserAccountTest {
	
	private Contact c1;
	private Contact c2;
	private UserAccount u1;
	private ArrayList<Contact> contactList;
	private ArrayList<Message> messageList;
	private MessageLog log;
	private Hashtable<Contact, MessageLog> hashLog;
	
	public void setup(){
		u1 = new UserAccount();
	}
	
	public void addAccountData(){
		u1.setUserName("HoneyBadger");
		u1.setFirstName("Joe");
		u1.setLastName("Doe");
		u1.setPassword("1234");
		u1.setPhone("333-333-3333");
		u1.setpId("11111");
		u1.setType(AccountType.STUDENT);
		u1.setYear(2012);
	}

	public void addContacts(){
		c1 = new Contact("12345", "John", "Doe", "555-555-5555", "johndoe@email.com", 2012, "johndoe123");
		c2 = new Contact("54321", "Jane", "Doe", "444-444-4444", "janedoe@email.com", 2012, "janedoe123");
		contactList = new ArrayList<Contact>();
		contactList.add(c1);
		contactList.add(c2);
		u1.setContacts(contactList);	
	}
	
	public void addFriends(){
		u1.addTOList(c1, ListCategory.FAVORITE);
	}
	
	public void addMessages(){
		log = new MessageLog();
		messageList = new ArrayList<Message>();
		hashLog = new Hashtable<Contact, MessageLog>();
		messageList.add(new Message("HoneyBadger", "janedoe123", "cool name", 
				"hey I just met you, and this is crazy, but here's my number, so call me maybe"));
		log.setMessages(messageList);
		hashLog.put(c2, log);
		u1.setMessages(hashLog);
	}

	@Test
	public void testGetUserName() {
		setup();
		addAccountData();
		assertEquals("HoneyBadger", u1.getUserName());		
	}
	
	@Test
	public void testGetNames() {
		setup();
		addAccountData();
		assertEquals("Joe", u1.getFirstName());
		assertEquals("Doe", u1.getLastName());
	}
	
	@Test
	public void testPID() {
		setup();
		addAccountData();
		assertEquals("11111", u1.getpId());
	}
	
	@Test
	public void testPhone() {
		setup();
		addAccountData();
		assertEquals("333-333-3333", u1.getPhone());
	}
	
	@Test
	public void testYear() {
		setup();
		addAccountData();
		assertEquals(2012, u1.getYear());
	}
	
	@Test
	public void testType() {
		setup();
		addAccountData();
		assertEquals(AccountType.STUDENT, u1.getType());
	}
	
	@Test
	public void testPassword() throws Exception {
		setup();
		addAccountData();
		assertTrue(Security.check("1234", u1.getPassword()));
	}
	
	
	@Test
	public void testGetContacts() {
		setup();
		addAccountData();
		addContacts();
		
		assertTrue(u1.getContacts() instanceof ArrayList);
		assertEquals(u1.getContacts().get(0), c1);
		assertEquals(u1.getContacts().get(1), c2);
		
		u1.getContacts().remove(0);
		assertEquals(u1.getContacts().get(0), c2);
	}

	@Test
	public void testGetMessages() {
		setup();
		addAccountData();
		addContacts();
		addMessages();
		
		assertTrue(u1.getMessages() instanceof Hashtable<?, ?>);
		assertEquals("hey I just met you, and this is crazy, but here's my number, so call me maybe",
				u1.getMessages().get(c2).getMessages().get(0).getBody());
		assertEquals("HoneyBadger",
				u1.getMessages().get(c2).getMessages().get(0).getTo());
		assertEquals("janedoe123",
				u1.getMessages().get(c2).getMessages().get(0).getFrom());
		assertEquals("cool name",
				u1.getMessages().get(c2).getMessages().get(0).getSubject());
	}
	
	@Test
	public void testGetFriends() {
		setup();
		addAccountData();
		addContacts();
		addFriends();
		
		assertTrue(u1.getFriends() instanceof ArrayList<?>);
		assertEquals("(0) 	[*            ]   John Doe", u1.getFriends().get(1));
		assertEquals("(1) 	[             ]   Jane Doe", u1.getFriends().get(2));
	}

}
